
export class Utility {
 
    public static isBlankList(input: any[]): Boolean {
        return input && input.length > 0;
    }
   
    public static isNotBlankList(input: any[]): Boolean {
        return !Utility.isBlankList(input);
    }
    
    public static deepCloneObj(originalObject: any, circular: boolean): any {
        // First create an empty object with
        // same prototype of our original source
        var propertyIndex, descriptor, keys, current, nextSource, indexOf,
            copies = [{
                source: originalObject,
                target: Array.isArray(originalObject) ? [] : Object.create(Object.getPrototypeOf(originalObject))
            }],
            cloneObject = copies[0].target,
            sourceReferences = [originalObject],
            targetReferences = [cloneObject];
        // First in, first out
        while (current = copies.shift())	// jshint ignore:line
        {
            keys = Object.getOwnPropertyNames(current.source);
            for (propertyIndex = 0; propertyIndex < keys.length; propertyIndex++) {
                // Save the source's descriptor
                descriptor = Object.getOwnPropertyDescriptor(current.source, keys[propertyIndex]);
                if (!descriptor.value || typeof descriptor.value !== 'object') {
                    Object.defineProperty(current.target, keys[propertyIndex], descriptor);
                    continue;
                }
                nextSource = descriptor.value;
                descriptor.value = Array.isArray(nextSource) ? [] : Object.create(Object.getPrototypeOf(nextSource));
                if (circular) {
                    indexOf = sourceReferences.indexOf(nextSource);
                    if (indexOf !== -1) {
                        // The source is already referenced, just assign reference
                        descriptor.value = targetReferences[indexOf];
                        Object.defineProperty(current.target, keys[propertyIndex], descriptor);
                        continue;
                    }
                    sourceReferences.push(nextSource);
                    targetReferences.push(descriptor.value);
                }
                Object.defineProperty(current.target, keys[propertyIndex], descriptor);
                copies.push({ source: nextSource, target: descriptor.value });
            }
        }
        return cloneObject;
    };

    /**
    * 拷贝对象  将fromObj的属性值copy到toObj中（toObj中没有对应的值才拷贝，有的话就不拷贝）
    * 用于修改和添加对象时候的对象赋值
    **/
    public static copyObj(toObj, fromObj) {
        for (let key of Object.keys(fromObj)) {
            if (fromObj.hasOwnProperty(key) && fromObj[key]) {
                toObj[key] = fromObj[key];
            }
        }
        return toObj;
    }

    /**
    * 拷贝对象  将fromObj的所有属性值copy到toObj中
    **/
    static copyObjAll(toObj, fromObj) {
        for (let key of Object.keys(fromObj)) {
            toObj[key] = fromObj[key];
        }
        return toObj;
    }
      /**
     * 判断终端设备
     */
    private static versions(): any {
        var u = navigator.userAgent;
        var app = navigator.appVersion;
        return {//移动终端浏览器版本信息   
            trident: u.indexOf('Trident') > -1, //IE内核  
            presto: u.indexOf('Presto') > -1, //opera内核  
            webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核  
            gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核  
            mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端  
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端  
            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器  
            iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器  
            iPad: u.indexOf('iPad') > -1, //是否iPad    
            webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部  
        }
    }

    /**
     * 区分设备，返回路由地址
     * json :{'pc':'/pcIdex','ipad':'/ipadIndex','mobile':'mobileIndex'}
     */
    public static equipmentJump(json?: any): any {
        if (json == null || (json['mobile'] == null && json['ipad'] == null && json['pc'] == null)) {
            if (this.versions().android || this.versions().iPhone) {
                return 'mobile';
            } else if (this.versions().iPad) {
                return 'ipad';
            } else {
                return 'pc';
            }
        } else {
            if (this.versions().android || this.versions().iPhone) {
                return json['mobile'];
            } else if (this.versions().iPad) {
                return json['ipad'];
            } else {
                return json['pc'];
            }
        }

    }
}